﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TWS.Client;

public partial class admin_ThayDoiLogo : BaseAdminPage
{
    protected void btnOK_OnClick(object sender, EventArgs e)
    {
        //Page.Validate();
        //if (!Page.IsValid) return;
        //BannerQuangCaoHelper spHP = new BannerQuangCaoHelper();
        //BannerQuangCaoDataSet spDS = new BannerQuangCaoDataSet();
        //BannerQuangCaoDataSet.BannerQuangCaoRow BannerQuangCaomoi = spDS.BannerQuangCao.NewBannerQuangCaoRow();
        //BannerQuangCaomoi.TenBannerQuangCao = txtTenBannerQuangCao.Text;
        //BannerQuangCaomoi.LoaiBannerQuangCao = Convert.ToInt32(ddlLoaiCon.SelectedValue);
        //BannerQuangCaomoi.HangSanXuat = txtHangSanXuat.Text;
        //BannerQuangCaomoi.ChungLoai = txtChungLoai.Text;
        //BannerQuangCaomoi.Gia = Convert.ToInt32(txtGia.Text);
        //////UploadImage();
        //if (txtHinhAnh.Text == string.Empty)
        //{
        //    txtHinhAnh.Text = "noimage.jpg";
        //}
        //BannerQuangCaomoi.HinhAnh = txtHinhAnh.Text;
        //BannerQuangCaomoi.BannerQuangCaoMoi = ckBannerQuangCaoMoi.Checked;
        //BannerQuangCaomoi.ThuTu = Convert.ToInt32(txtThuTu.Text);
        //BannerQuangCaomoi.NgayDang = DateTime.Now;

        //BannerQuangCaomoi.TongQuan = txtTongQuan.Text;
        //BannerQuangCaomoi.ThongSoKyThuat = txtThongSo.Text;
        //BannerQuangCaomoi.Album = txtAlbum.Text;
        //spDS.BannerQuangCao.AddBannerQuangCaoRow(BannerQuangCaomoi);
        //spHP.SaveBannerQuangCao(spDS);
        //RedirectPage(Request.RawUrl, true);
        SaveData();
    }

    private BannerQuangCaoDataSet m_BannerQuangCaoDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {

        txtHinhAnh.Text = CauHinhHelper.GetCauHinh("Logo");
        string saveLocation = MyFilePathHelper.GetImageFolderURL(this);
        this.imageDemons.Src = saveLocation + txtHinhAnh.Text;
    }

    private void SaveData()
    {
        try
        {
            
            UploadImage();
            UploadFlash();
            UploadSlogan();

        }
        catch (Exception ex)
        {
            //DisplayMesseage(ex.Message);
            this.RedirectPage("pgError.aspx", false);
        }
        RedirectPage(Request.RawUrl, true);
    }

    private void UploadImage()
    {
        if (chkDeleteFile.Checked && txtHinhAnh.Text != string.Empty)
        {
            //delete old image
            try
            {
                string saveLocation = MyFilePathHelper.GetImageFolderURL_Saving(this);
                MyFilePathHelper.DeleteResource(saveLocation, txtHinhAnh.Text);
            }
            catch (Exception)
            {
            }
            finally
            {
                txtHinhAnh.Text = "noimage.jpg";
            }
            //postpone new image
        }
        //save new image
        if (this.inputFile.PostedFile != null && this.inputFile.PostedFile.ContentLength > 0)
        {
            string fn = System.IO.Path.GetFileName(inputFile.PostedFile.FileName);
            string path = MyFilePathHelper.GetImageFolderURL_Saving(this);
            fn = MyFilePathHelper.AppendTimeStamp(fn);
            string saveLocation = path + fn;

            inputFile.PostedFile.SaveAs(saveLocation);
            FileStream fs = File.Open(saveLocation, FileMode.Open, FileAccess.Read);
            byte[] inByte = new byte[fs.Length];
            fs.Read(inByte, 0, inByte.Length);
            fs.Close();
            MyFilePathHelper.DeleteResource(path, fn);
            ImagesConvertHelper iconvert = new ImagesConvertHelper();
            string filename;
            if (iconvert.ConvertImage(saveLocation, inByte, 6, out filename) == false)
                txtHinhAnh.Text = string.Empty;
            else
            {
                txtHinhAnh.Text = fn + ".JPG";
            }
            CauHinhDataSet chDS = new CauHinhHelper().LoadCauHinh("Logo");
            chDS.CauHinh[0].ThongSo1 = txtHinhAnh.Text;
            new CauHinhHelper().SaveCauHinh(chDS);
        }
    }

    private void UploadFlash()
    {
        // Check if a file was submitted.
        if (Uploader.PostedFile.ContentLength != 0)
        {
            try
            {
                if (Uploader.PostedFile.ContentLength > 1048576)
                {
                    // This exceeds the size limit you want to allow (1 MB).
                    // You can also use the maxRequestLength attribute
                    // of the httpRuntime element (in the web.config file)
                    // to refuse large requests altogether.
                    //lblStatus.Text = "Too large. This file is not allowed";
                }
                else
                {
                    // Retrieve the physical directory path for the Upload
                    // subdirectory.
                    string destDir = MyFilePathHelper.GetImageFolderURL_Saving(this);
                    // Extract the filename part from the full path of the
                    // original file.
                    string fileName = MyFilePathHelper.AppendTimeStamp(Path.GetFileName(Uploader.PostedFile.FileName));
                    // Combine the destination directory with the filename.
                    string destPath = Path.Combine(destDir, fileName);
                    // Save the file on the server.
                    Uploader.PostedFile.SaveAs(destPath);
                    //lblStatus.Text = "Thanks for submitting your file.";
                    CauHinhDataSet chDS = new CauHinhHelper().LoadCauHinh("Flash");
                    chDS.CauHinh[0].ThongSo1 = fileName;
                    new CauHinhHelper().SaveCauHinh(chDS);
                }
            }
            catch (Exception err)
            {
                //lblStatus.Text = err.Message;
            }
        }
    }

    private void UploadSlogan()
    {
        // Check if a file was submitted.
        if (SloganUP.PostedFile.ContentLength != 0)
        {
            try
            {
                if (SloganUP.PostedFile.ContentLength > 1048576)
                {
                    // This exceeds the size limit you want to allow (1 MB).
                    // You can also use the maxRequestLength attribute
                    // of the httpRuntime element (in the web.config file)
                    // to refuse large requests altogether.
                    //lblStatus.Text = "Too large. This file is not allowed";
                }
                else
                {
                    // Retrieve the physical directory path for the Upload
                    // subdirectory.
                    string destDir = MyFilePathHelper.GetImageFolderURL_Saving(this);
                    // Extract the filename part from the full path of the
                    // original file.
                    string fileName = MyFilePathHelper.AppendTimeStamp(Path.GetFileName(SloganUP.PostedFile.FileName));
                    // Combine the destination directory with the filename.
                    string destPath = Path.Combine(destDir, fileName);
                    // Save the file on the server.
                    SloganUP.PostedFile.SaveAs(destPath);
                    //lblStatus.Text = "Thanks for submitting your file.";
                    CauHinhDataSet chDS = new CauHinhHelper().LoadCauHinh("Slogan");
                    chDS.CauHinh[0].ThongSo1 = fileName;
                    new CauHinhHelper().SaveCauHinh(chDS);
                }
            }
            catch (Exception err)
            {
                //lblStatus.Text = err.Message;
            }
        }
    }
}